{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "first-commission",
   "metadata": {},
   "source": [
    "# German Personal Tax Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "internal-camcorder",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "wicked-victoria",
   "metadata": {},
   "source": [
    "The function `clean_de_idnr()` cleans a column containing German personal tax number (IDNR) strings, and standardizes them in a given format. The function `validate_de_idnr()` validates either a single IDNR strings, a column of IDNR strings or a DataFrame of IDNR strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "psychological-quebec",
   "metadata": {},
   "source": [
    "IDNR strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"36574261809\"\n",
    "* `standard`: IDNR strings with proper whitespace in the proper places, like \"36 574 261 809\"\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_de_idnr()` and `validate_de_idnr()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "refined-pressure",
   "metadata": {},
   "source": [
    "### An example dataset containing IDNR strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "rural-delicious",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"idnr\": [\n",
    "            \"36574261809\",\n",
    "            \"36554266806\",\n",
    "            \"51824753556\",\n",
    "            \"51 824 753 556\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\"\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "peaceful-richmond",
   "metadata": {},
   "source": [
    "## 1. Default `clean_de_idnr`\n",
    "\n",
    "By default, `clean_de_idnr` will clean idnr strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dietary-bishop",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_de_idnr\n",
    "clean_de_idnr(df, column = \"idnr\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adopted-morgan",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sufficient-medline",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "played-anthony",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "polished-philippines",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_de_idnr(df, column = \"idnr\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "criminal-satisfaction",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "criminal-demographic",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "clean_de_idnr(df, column = \"idnr\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "forced-holmes",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned IDNR strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "international-rates",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_de_idnr(df, column=\"idnr\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sonic-notice",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "satisfactory-munich",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "compliant-cradle",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_de_idnr(df, \"idnr\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "chronic-static",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "induced-herald",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_de_idnr(df, \"idnr\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "suited-spouse",
   "metadata": {},
   "source": [
    "## 4. `validate_de_idnr()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "coordinated-tanzania",
   "metadata": {},
   "source": [
    "`validate_de_idnr()` returns `True` when the input is a valid IDNR. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_de_idnr()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_de_idnr()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_de_idnr()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "scenic-semester",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_de_idnr\n",
    "print(validate_de_idnr(\"36574261809\"))\n",
    "print(validate_de_idnr(\"36554266806\"))\n",
    "print(validate_de_idnr(\"51824753556\"))\n",
    "print(validate_de_idnr(\"51 824 753 556\"))\n",
    "print(validate_de_idnr(\"hello\"))\n",
    "print(validate_de_idnr(np.nan))\n",
    "print(validate_de_idnr(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "after-telephone",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bored-enlargement",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_de_idnr(df[\"idnr\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "explicit-timothy",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "loving-temperature",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_de_idnr(df, column=\"idnr\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "collected-breach",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "chinese-closure",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_de_idnr(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aware-finger",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
